package com.shuorikeji.huohuo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.bean.BookFlInfoBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class TjTypeAdapter extends RecyclerView.Adapter<TjTypeAdapter.BaseViewHolder> {
    private final Context mContext;
    private List<BookFlInfoBean> mData = new ArrayList<>();
    private ClickCallBack callBack;

    public TjTypeAdapter(Context context, ClickCallBack callBackFlag) {
        this.mContext = context;
        this.callBack = callBackFlag;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.tj_type_item_layout, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvName.setText(mData.get(position).getName());
        holder.tvName.setTextColor((mData.get(position).isSec()) ? mContext.getResources().getColor(R.color.color_333333) :
                mContext.getResources().getColor(R.color.color_666666));
        holder.llMain.setBackgroundColor((mData.get(position).isSec()) ? mContext.getResources().getColor(R.color.white_color) :
                mContext.getResources().getColor(R.color.color_F8F8F8));
        holder.viewSec.setVisibility((mData.get(position).isSec()) ? View.VISIBLE : View.INVISIBLE);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.onClick(1, mData.get(position));
                }
            }
        });
    }

    public void setData(List<BookFlInfoBean> data) {
        if (mData != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        View viewSec;
        TextView tvName;
        LinearLayout llMain;

        public BaseViewHolder(View itemView) {
            super(itemView);
            viewSec = itemView.findViewById(R.id.view_sec);
            tvName = itemView.findViewById(R.id.tv_type);
            llMain = itemView.findViewById(R.id.ll_main);
        }
    }
}
